/*
 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 * 
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
/*
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Rick Macklem at The University of Guelph.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
/*
 * This file includes modifications of code found in the Apple XNU Kernel
 * and has been modifed for use in this Kext by Rick Macklem, May 2006.
 */

/*
 * generally, I don't like #includes inside .h files, but it seems to
 * be the easiest way to handle the port.
 */
#ifndef APPLEKEXT
#include <newnfs/nfs/nfsport.h>
#include <netinet/if_ether.h>
#include <net/if_types.h>
#include <net/kpi_interface.h>

extern struct nfsnodehashhead *newnfsnodehashtbl;
extern u_long newnfsnodehash;
extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
extern vnop_t **newnfs_vnodeop_p;
extern vnop_t **fifo_newnfsnodeop_p;
extern vnop_t **spec_newnfsnodeop_p;
extern int ncl_mbuf_mlen;
extern int nfsrv_debug;
extern int nfscl_inited;
extern lck_mtx_t *ncl_node_hash_mutex;
extern lck_spin_t *ncl_nfsnode_slock;
extern struct nfsgssclsockhead nfsgssclsockhead;
extern struct newnfsd_head newnfscbd_head;
extern int newnfscbd_head_flag;
extern struct newnfs_sockhead newnfscb_sockhead;
extern int newnfscb_sockhead_flag;
NFSDLOCKMUTEX;

LIST_HEAD(nfsnodehashhead, nfsnode);
struct nfssvc_sock *newnfs_cbudpsock;
lck_mtx_t *nfs_clstate_mutex;
lck_mtx_t *nfs_gsscl_mutex;
lck_spin_t *ncl_nfsnode_slock;

struct nfsrenewthread {
	LIST_ENTRY(nfsrenewthread)	next;
	struct nfsclclient		*clp;
};
LIST_HEAD(nfsrenewthreadhead, nfsrenewthread) nfsrenewthreadhead;

kern_return_t	thread_terminate(thread_t);

#define FSDBG(A, B, C, D, E) \
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
		(int)(B), (int)(C), (int)(D), (int)(E), 0)
#define FSDBG_TOP(A, B, C, D, E) \
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
		(int)(B), (int)(C), (int)(D), (int)(E), 0)
#define FSDBG_BOT(A, B, C, D, E) \
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
		(int)(B), (int)(C), (int)(D), (int)(E), 0)
#endif	/* !APPLEKEXT */

/*
 * Define all the client side locks here, so they can be initialized
 * by nfscl_init().
 */
static lck_grp_attr_t *nfs_clstate_lck_grp_attr;
static lck_grp_t *nfs_clstate_lck_grp;
static lck_attr_t *nfs_clstate_lck_attr;
static lck_grp_attr_t *nfs_gsscl_lck_grp_attr;
static lck_grp_t *nfs_gsscl_lck_grp;
static lck_attr_t *nfs_gsscl_lck_attr;
static lck_grp_attr_t *nfscl_renew_lck_grp_attr;
static lck_grp_t *nfscl_renew_lck_grp;
static lck_attr_t *nfscl_renew_lck_attr;
static lck_spin_t *nfscl_renew_slock;
static lck_grp_attr_t *ncl_nfsnode_lck_grp_attr;
static lck_grp_t *ncl_nfsnode_lck_grp;
static lck_attr_t *ncl_nfsnode_lck_attr;

/*
 * Look up a vnode/nfsnode by file handle.
 * Callers must check for mount points!!
 * In all cases, a pointer to a
 * nfsnode structure is returned.
 * This variant takes a "struct nfsfh *" as second argument and uses
 * that structure up, either by hanging off the nfsnode or FREEing it.
 */
APPLESTATIC int
nfscl_nget(struct mount *mntp, struct vnode *dvp, struct nfsfh *nfhp,
    struct componentname *cnp, __unused NFSPROC_T *p, struct nfsnode **npp, void *stuff)
{
	struct nfsnode *np, *dnp;
	struct nfsnodehashhead *nhpp;
	vnode_t vp, nvp;
	int error;
	mount_t mp;
	struct vnode_fsparam vfsp;
	uint32_t vid;
	struct nfsattrstuff *asp = (struct nfsattrstuff *)stuff;
	struct nfsv4node *n4 = NULL;
	struct nfsmount *nmp;

	/* Check for unmount in progress */
	if (!mntp || (mntp->mnt_kern_flag & MNTK_UNMOUNT)) {
		*npp = NULL;
		FREE((caddr_t)nfhp, M_NFSFH);
		return (!mntp ? ENXIO : EPERM);
	}

	nmp = VFSTONFS(mntp);
	dnp = VTONFS(dvp);
	if (nmp->nm_flag & NFSMNT_NFSV4)
		/*
		 * Do the malloc now, so there is no potential sleep/race
		 * later. Free it, if we don't need it.
		 */
		MALLOC(n4, struct nfsv4node *,
		    sizeof (struct nfsv4node) +
		    dnp->n_fhp->nfh_len + cnp->cn_namelen - 1,
		    M_NFSV4NODE, M_WAITOK);
	nhpp = NFSNOHASH(ncl_hash(nfhp->nfh_fh, nfhp->nfh_len));
loop:
	lck_mtx_lock(ncl_node_hash_mutex);
	for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
		mp = (np->n_flag & NINIT) ? np->n_mount : vnode_mount(NFSTOV(np));
DEBUG1PRINTF("nget mntp=0x%x mp=0x%x nf=0x%x fl=%d fl2=%d fh=0x%x fh2=0x%x\n",mntp,mp,np->n_flag,np->n_fhp->nfh_len,nfhp->nfh_len,np->n_fhp->nfh_fh[12],nfhp->nfh_fh[12]);
		if (mntp != mp || np->n_fhp->nfh_len != nfhp->nfh_len ||
		    NFSBCMP(nfhp->nfh_fh, np->n_fhp->nfh_fh, nfhp->nfh_len))
			continue;
		/* if the node is still being initialized, sleep on it */
		if (np->n_flag & NINIT) {
			OSBitOrAtomic((u_int32_t)NWINIT, (UInt32 *)&np->n_flag);
			msleep(np, ncl_node_hash_mutex, PDROP | PINOD, "nfs_nget", NULL);
			goto loop;
		}
		vp = NFSTOV(np);
		vid = vnode_vid(vp);
		lck_mtx_unlock(ncl_node_hash_mutex);
		if ((error = vnode_getwithvid(vp, vid))) {
			FREE((caddr_t)nfhp, M_NFSFH);
			if (n4 != NULL)
				FREE((caddr_t)n4, M_NFSV4NODE);
			/*
			 * If vnode is being reclaimed or has already
			 * changed identity, no need to wait.
			 */
			return (error);
		} 
		/*
		 * For NFSv4, check to see if it is the same name and
		 * replace the name, if it is different.
		 */
		if ((nmp->nm_flag & NFSMNT_NFSV4) &&
		    np->n_v4 != NULL && vp->v_type == VREG &&
		    (np->n_v4->n4_namelen != cnp->cn_namelen ||
		      NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
			 cnp->cn_namelen) ||
		      dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
		      NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
			dnp->n_fhp->nfh_len))) {
			FREE((caddr_t)np->n_v4, M_NFSV4NODE);
			np->n_v4 = n4;
			n4 = NULL;
			np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
			np->n_v4->n4_namelen = cnp->cn_namelen;
			NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
			    dnp->n_fhp->nfh_len);
			NFSBCOPY(cnp->cn_nameptr,
			    NFS4NODENAME(np->n_v4), cnp->cn_namelen);
		}
		/* update attributes */
		error = nfscl_loadattrcache(np, asp->vap, NULL, &asp->xid, 0);
		if (error) {
			vnode_put(vp);
		} else {
			if (dvp && cnp && (asp->flags & NG_MAKEENTRY))
				cache_enter(dvp, vp, cnp);
			*npp = np;
		}
		FREE((caddr_t)nfhp, M_NFSFH);
		if (n4 != NULL)
			FREE((caddr_t)n4, M_NFSV4NODE);
		return(error);
	}

	/*
	 * allocate and initialize nfsnode and stick it in the hash
	 * before calling getnewvnode().  Anyone finding it in the
	 * hash before initialization is complete will wait for it.
	 */
	MALLOC_ZONE(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
	if (!np) {
		lck_mtx_unlock(ncl_node_hash_mutex);
		FREE((caddr_t)nfhp, M_NFSFH);
		if (n4 != NULL)
			FREE((caddr_t)n4, M_NFSV4NODE);
		*npp = NULL;
		return (ENOMEM);
	}
	bzero((caddr_t)np, sizeof *np);
	np->n_flag |= NINIT;
	np->n_mount = mntp;

	/* setup node's file handle */
	np->n_fhp = nfhp;

	/* Insert the nfsnode in the hash queue for its new file handle */
	np->n_flag |= NHASHED;
	LIST_INSERT_HEAD(nhpp, np, n_hash);

	/* release lock on hash table */
	lck_mtx_unlock(ncl_node_hash_mutex);

	/* do initial loading of attributes */
	error = nfscl_loadattrcache(np, asp->vap, NULL, &asp->xid, 1);
	if (error) {
		lck_mtx_lock(ncl_node_hash_mutex);
		LIST_REMOVE(np, n_hash);
		OSBitAndAtomic((int32_t)~(NHASHED | NINIT), (UInt32 *)&np->n_flag);
		if (np->n_flag & NWINIT) {
			OSBitAndAtomic((int32_t)~NWINIT, (UInt32 *)&np->n_flag);
			wakeup((caddr_t)np);
		}
		lck_mtx_unlock(ncl_node_hash_mutex);
		FREE_ZONE(np, sizeof *np, M_NFSNODE);
		FREE((caddr_t)nfhp, M_NFSFH);
		if (n4 != NULL)
			FREE((caddr_t)n4, M_NFSV4NODE);
		*npp = NULL;
		return (error);
	}
	np->n_mtime = asp->vap->na_vattr.nva_mtime;
	if (asp->vap->na_vattr.nva_type == VDIR)
		np->n_ncmtime = asp->vap->na_vattr.nva_mtime;
	NMODEINVALIDATE(np);

	/* now, attempt to get a new vnode */
	vfsp.vnfs_mp = mntp;
	vfsp.vnfs_vtype = asp->vap->na_vattr.nva_type;
	vfsp.vnfs_str = "newnfs";
	vfsp.vnfs_dvp = dvp;
	vfsp.vnfs_fsnode = np;
	if (asp->vap->na_vattr.nva_type == VFIFO)
		vfsp.vnfs_vops = fifo_newnfsnodeop_p;
	else if (asp->vap->na_vattr.nva_type == VBLK ||
		 asp->vap->na_vattr.nva_type == VCHR)
		vfsp.vnfs_vops = spec_newnfsnodeop_p;
	else
		vfsp.vnfs_vops = newnfs_vnodeop_p;
	/* 
	 * Are we getting the root? If so, make sure the vnode flags
	 * are correct 
	 */
	if ((nfhp->nfh_len == nmp->nm_fhsize) &&
	    !bcmp(nfhp->nfh_fh, nmp->nm_fh, nfhp->nfh_len)) {
		vfsp.vnfs_markroot = 1;
	} else {
		vfsp.vnfs_markroot = 0;
	}
	
	vfsp.vnfs_marksystem = 0;
	vfsp.vnfs_rdev = 0;
	vfsp.vnfs_filesize = asp->vap->na_vattr.nva_size;
	vfsp.vnfs_cnp = cnp;
	if (dvp && cnp && (asp->flags & NG_MAKEENTRY))
		vfsp.vnfs_flags = 0;
	else
		vfsp.vnfs_flags = VNFS_NOCACHE;
	error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &nvp);
	if (error) {
		lck_mtx_lock(ncl_node_hash_mutex);
		LIST_REMOVE(np, n_hash);
		OSBitAndAtomic((int32_t)~(NHASHED | NINIT), (UInt32 *)&np->n_flag);
		if (np->n_flag & NWINIT) {
			OSBitAndAtomic((int32_t)~NWINIT, (UInt32 *)&np->n_flag);
			wakeup((caddr_t)np);
		}
		lck_mtx_unlock(ncl_node_hash_mutex);
		FREE_ZONE(np, sizeof *np, M_NFSNODE);
		FREE((caddr_t)nfhp, M_NFSFH);
		if (n4 != NULL)
			FREE((caddr_t)n4, M_NFSV4NODE);
		*npp = NULL;
		return (error);
	}

	/*
	 * For NFSv4, we have to attach the directory file handle and
	 * file name, so that Open Ops can be done later.
	 */
	if (nmp->nm_flag & NFSMNT_NFSV4) {
		np->n_v4 = n4;
		n4 = NULL;
		np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
		np->n_v4->n4_namelen = cnp->cn_namelen;
		NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
		    dnp->n_fhp->nfh_len);
		NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
		    cnp->cn_namelen);
	}

	vp = nvp;
	np->n_vnode = vp;
	vnode_addfsref(vp);
	vnode_settag(vp, VT_NEWNFS); // XXX shouldn't this be a vnode_create() parameter?
	*npp = np;
	/* node is now initialized */

	/* check if anyone's waiting on this node */
	lck_mtx_lock(ncl_node_hash_mutex);
	OSBitAndAtomic((int32_t)~NINIT, (UInt32 *)&np->n_flag);
	if (np->n_flag & NWINIT) {
		OSBitAndAtomic((int32_t)~NWINIT, (UInt32 *)&np->n_flag);
		wakeup((caddr_t)np);
	}
	lck_mtx_unlock(ncl_node_hash_mutex);

	if (n4 != NULL)
		FREE((caddr_t)n4, M_NFSV4NODE);
	return (error);
}

/*
 * Anothe variant of nfs_nget(). This one is only used by reopen. It
 * takes almost the same args as nfs_nget(), but only succeeds if an entry
 * exists in the cache. (Since files should already be "open" with a
 * vnode ref cnt on the node when reopen calls this, it should always
 * succeed.)
 */
APPLESTATIC int
nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize,
    __unused NFSPROC_T *p, struct nfsnode **npp)
{
	struct nfsnode *np;
	struct nfsnodehashhead *nhpp;
	mount_t mp;
	struct nfsmount *nmp;
	vnode_t vp;
	u_int32_t vid;
	int error = 0;

	/* Check for unmount in progress */
	if (mntp == NULL || (mntp->mnt_kern_flag & MNTK_UNMOUNT)) {
		*npp = NULL;
		return ((mntp == NULL) ? ENXIO : EPERM);
	}

	nmp = VFSTONFS(mntp);
	nhpp = NFSNOHASH(ncl_hash(fhp, fhsize));
loop:
	lck_mtx_lock(ncl_node_hash_mutex);
	for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
		mp = (np->n_flag & NINIT) ? np->n_mount : vnode_mount(NFSTOV(np));
		if (mntp != mp || np->n_fhp->nfh_len != fhsize ||
		    NFSBCMP(fhp, np->n_fhp->nfh_fh, fhsize) ||
		    np->n_v4 == NULL)
			continue;
		/* if the node is still being initialized, sleep on it */
		if (np->n_flag & NINIT) {
			OSBitOrAtomic((u_int32_t)NWINIT, (UInt32 *)&np->n_flag);
			msleep(np, ncl_node_hash_mutex, PDROP | PINOD, "nfs_nget", NULL);
			goto loop;
		}
		vp = NFSTOV(np);
		vid = vnode_vid(vp);
		lck_mtx_unlock(ncl_node_hash_mutex);
		error = vnode_getwithvid(vp, vid);
		if (!error)
			*npp = np;
		return (error);
	}
	lck_mtx_unlock(ncl_node_hash_mutex);
	return (EINVAL);
}

/*
 * Load the attribute cache (that lives in the nfsnode entry) with
 * the attributes of the second argument and
 * Iff vaper not NULL
 *    copy the attributes to *vaper
 * Similar to nfs_loadattrcache(), except that the arguments are different
 * and it optionally does the copy that was in nfsm_loadattr().
 */
APPLESTATIC int
nfscl_loadattrcache(struct nfsnode *np, struct nfsvattr *nvap,
    struct nfs_vattr *newvap, u_int64_t *xidp, int attrinfo)
{
	mount_t mp;
	vnode_t vp;
	struct nfsmount *nmp;
	struct timeval now;
	struct nfsvattr *npnvap;

	if (np->n_flag & NINIT) {
		vp = NULL;
		mp = np->n_mount;
		nmp = NULL;
	} else {
		vp = NFSTOV(np);
		mp = vnode_mount(vp);
		nmp = VFSTONFS(mp);
	}

	FSDBG_TOP(527, vp, np, *xidp >> 32, *xidp);

	if (!VFSTONFS(mp)) {
		FSDBG_BOT(527, ENXIO, 1, 0, *xidp);
		return (ENXIO); 
	}

	if (*xidp < np->n_xid) {
		/*
		 * We have already updated attributes with a response from
		 * a later request.  The attributes we have here are probably
		 * stale so we drop them (just return).  However, our 
		 * out-of-order receipt could be correct - if the requests were
		 * processed out of order at the server.  Given the uncertainty
		 * we invalidate our cached attributes.  *xidp is zeroed here
		 * to indicate the attributes were dropped - only getattr
		 * cares - it needs to retry the rpc.
		 */
		NATTRINVALIDATE(np);
		FSDBG_BOT(527, 0, np, np->n_xid, *xidp);
		*xidp = 0;
		return (0);
	}

	if (vp && (nvap->na_vattr.nva_type != vnode_vtype(vp))) {
		/*
		 * The filehandle has changed type on us.  This can be
		 * caused by either the server not having unique filehandles
		 * or because another client has removed the previous
		 * filehandle and a new object (of a different type)
		 * has been created with the same filehandle.
		 *
		 * We can't simply switch the type on the vnode because
		 * there may be type-specific fields that need to be
		 * cleaned up or set up.
		 *
		 * So, what should we do with this vnode?
		 *
		 * About the best we can do is log a warning and return
		 * an error.  ESTALE is about the closest error, but it
		 * is a little strange that we come up with this error
		 * internally instead of simply passing it through from
		 * the server.  Hopefully, the vnode will be reclaimed
		 * soon so the filehandle can be reincarnated as the new
		 * object type.
		 */
		printf("nfs loadattrcache vnode changed type, was %d now %d\n",
			vnode_vtype(vp), nvap->na_vattr.nva_type);
		FSDBG_BOT(527, ESTALE, 3, 0, *xidp);
		return (ESTALE);
	}

	microuptime(&now);
	np->n_attrstamp = now.tv_sec;
	np->n_xid = *xidp;

	npnvap = &np->n_vattr;
	/*
	 * For NFSv4, if the node's fsid is not equal to the mount point's
	 * fsid, return the low order 32bits of the node's fsid. This
	 * allows getcwd(3) to work. There is a chance that the fsid might
	 * be the same as a local fs, but since this is in an NFS mount
	 * point, I don't think that will cause any problems?
	 */
	if (nmp != NULL && (nmp->nm_flag & (NFSMNT_NFSV4 | NFSMNT_HASSETFSID)) ==
	    (NFSMNT_NFSV4 | NFSMNT_HASSETFSID) &&
	    (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
	     nmp->nm_fsid[1] != np->n_vattr.na_filesid[1]))
		nvap->na_fsid = np->n_vattr.na_filesid[0];
	else
		nvap->na_fsid = vfs_statfs(mp)->f_fsid.val[0];

	/*
	 * If attrinfo == 2 this is a write RPC for NFSv4 and only certain
	 * attributes are available.
	 */
	if (attrinfo == 2) {
		npnvap->na_filerev = nvap->na_filerev;
		npnvap->na_size = nvap->na_size;
		npnvap->na_mtime = nvap->na_mtime;
		npnvap->na_ctime = nvap->na_ctime;
		npnvap->na_fsid = nvap->na_fsid;
	} else {
		bcopy((caddr_t)nvap, (caddr_t)npnvap, sizeof (struct nfsvattr));
	}

	if (vp) {
		if (nvap->na_size != np->n_size) {
			FSDBG(527, vp, nvap->na_size, np->n_size,
			      (nvap->na_type == VREG) |
			      (np->n_flag & NMODIFIED ? 6 : 4));
			if (nvap->na_type == VREG) {
				u_int64_t orig_size;
				int cnt = 0;

				/*
				 * On rare occations n_size changes while in the ubc_setsize()
				 * call. To deal with this I've added a loop with cnt as a
				 * sanity check to avoid any possibility of an infinite loop.
				 */
				do {
					orig_size = np->n_size;
					lck_spin_lock(ncl_nfsnode_slock);
					if (np->n_flag & NMODIFIED) {
						if (nvap->na_size < np->n_size)
							nvap->na_size = np->n_size;
						else
							np->n_size = nvap->na_size;
					} else
						np->n_size = nvap->na_size;
					lck_spin_unlock(ncl_nfsnode_slock);
					if (!UBCINFOEXISTS(vp) ||
					    (attrinfo && np->n_size < (u_quad_t)ubc_getsize(vp))) {
						nvap->na_size = np->n_size = orig_size;
						NATTRINVALIDATE(np);
					} else {
						orig_size = np->n_size;
						ubc_setsize(vp, (off_t)np->n_size); /* XXX */
					}
#ifdef DIAGNOSTIC
					if (np->n_size != orig_size)
						printf("nfscl_loadattrcache: size ns=%qd os=%qd\n",
						    np->n_size, orig_size);
#endif
				} while (np->n_size != orig_size && cnt++ < 5);
			} else
				np->n_size = nvap->na_size;
		}
	} else {
		np->n_size = nvap->na_size;
	}

	if (np->n_flag & NCHG) {
		if (np->n_flag & NACC)
			nvap->na_atime = np->n_atim;
		if (np->n_flag & NUPD)
			nvap->na_mtime = np->n_mtim;
	}

	/*
	 * If newvap != NULL, copy out to it.
	 */
	if (newvap != NULL)
		bcopy((caddr_t)&nvap->na_vattr, (caddr_t)newvap,
		    sizeof (struct nfs_vattr));

	FSDBG_BOT(527, 0, np, 0, *xidp);
	return (0);
}

/*
 * Fill in the client id name. For these bytes:
 * 1 - they must be unique
 * 2 - they should be persistent across client reboots
 * 1 is more critical than 2
 * Achieving the above is very much architecture dependent, but here is
 * what this code does:
 * - puts the server IP addr in the name
 *   (Putting the IP addr of the server in the idname ensures that the
 *    names will be different for a server, if the client mounts with
 *    different IP addrs to the same server. In other words, the client
 *    thinks it has mounted different servers and the server thinks the
 *    mounts are from different clients, so everybody is happy.)
 * - the MAC address of the first net interface, if found
 *   (A net interface might move to a different box, but hopefully will
 *    not be used by multiple clients concurrently. That is sufficient
 *    to retain the uniqueness property required.)
 * OR
 * - random bytes to try and ensure uniqueness, if the MAC address isn't
 *   available (Not a good thing, but 1 is more critical than 2, so ...)
 */
APPLESTATIC void
nfscl_fillclid(u_int64_t clval, u_int8_t *cp, union nethostaddr *haddrp)
{
	int cnt;
	struct ifnet *ifp;

	/*
	 * First, put in the 64bit mount point identifier.
	 */
	NFSBCOPY((caddr_t)&clval, cp, 8);
	cp += 8;
	cnt = 8;

	/*
	 * Now, put in the server IP addr.
	 * XXX - should do IPv6 as well
	 */
	NFSBCOPY((caddr_t)&haddrp->had_inet.s_addr, cp, 4);
	cp += 4;
	cnt += 4;

	/*
	 * Should it handle addresses that aren't 6 bytes?
	 */
	TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
		if (!strcmp(ifp->if_name, "en") &&
		    ifnet_lladdr_copy_bytes((ifnet_t)ifp, cp, 6) == 0) {
			cp += 6;
			cnt += 6;
			break;
		}
	}

	/*
	 * This loop will only execute if the MAC address wasn't found.
	 */
	while (cnt < NFSV4CL_CLIDLEN) {
		*cp++ = (u_int8_t)(arc4random() % 256);
		cnt++;
	}
}

/*
 * Fill in a lock owner name. For now, pid + the process's creation time.
 */
APPLESTATIC void
nfscl_filllockowner(NFSPROC_T *p, u_int8_t *cp)
{
	union {
		u_int32_t	lval;
		u_int8_t	cval[4];
	} tl;

if (p == NULL) {
	printf("NULL pid\n");
	p = current_proc();
}
	tl.lval = proc_pid(p);
	*cp++ = tl.cval[0];
	*cp++ = tl.cval[1];
	*cp++ = tl.cval[2];
	*cp++ = tl.cval[3];
	tl.lval = p->p_stats->p_start.tv_sec;
	*cp++ = tl.cval[0];
	*cp++ = tl.cval[1];
	*cp++ = tl.cval[2];
	*cp++ = tl.cval[3];
	tl.lval = p->p_stats->p_start.tv_usec;
	*cp++ = tl.cval[0];
	*cp++ = tl.cval[1];
	*cp++ = tl.cval[2];
	*cp = tl.cval[3];
}

/*
 * Get the parent process for the one passed in as an argument.
 * If none exists, return NULL.
 * The value of the pointer is just used as an owner name, so
 * holding a ref cnt on it isn't required.
 */
APPLESTATIC NFSPROC_T *
nfscl_getparent(NFSPROC_T *p)
{
	int ppid;
	proc_t pp = NULL;

	if (p == NULL)
		return (NULL);
	proc_list_lock();
	if (proc_pid(p) != 0) {
		ppid = proc_ppid(p);
		if (ppid != 0)
			pp = pfind_locked(ppid);
	}
	proc_list_unlock();
	return (pp);
}

/*
 * Since an argument can't be passed into a new kernel thread, use a linked
 * list to pass the nfsclclient ptr to it.
 */

/*
 * Called to start up a renew thread. (Used for AUTH_SYS mounts.)
 */
static void
start_nfscl(void)
{
	struct nfsclclient *clp;
	struct nfsrenewthread *rtp;

	lck_spin_lock(nfscl_renew_slock);
	rtp = LIST_FIRST(&nfsrenewthreadhead);
	if (rtp != LIST_END(&nfsrenewthreadhead)) {
		LIST_REMOVE(rtp, next);
		lck_spin_unlock(nfscl_renew_slock);
		clp = rtp->clp;
		clp->nfsc_renewthread = NULL;
		FREE(rtp, M_NFSRENEWTHREAD);
		nfscl_renewthread(clp, clp->nfsc_renewthread);
	} else {
		lck_spin_unlock(nfscl_renew_slock);
	}
	(void) thread_terminate(current_thread());
}

/*
 * Called by nfssvc() to provide a bsd process/thread that can be used
 * for a renew thread for Kerberized mounts. For gssd upcalls to work,
 * a kernel thread isn't sufficient.
 */
static int
nfscl_renewproc(proc_t p, struct nfssvc_args *uap)
{
	struct nfsclclient *clp;
	struct nfsrenewthread *rtp;
	struct vfsstatfs *vfsp;
	mount_t mp;
	struct nfsmount *nmp;
	vnode_t vp;
	int error;
	struct nameidata nd;
	char path[MAXPATHLEN];
	u_int pathlen;

	error = copyinstr(uap->argp, path, MAXPATHLEN, (size_t *)&pathlen);
	if (error)
		return (error);

	/*
	 * Loop around until unmounted or a renew structure acquired.
	 */
	do {
		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
		    CAST_USER_ADDR_T(path), vfs_context_current());
		error = namei(&nd);
		if (error)
			/* If the mount point doesn't exist, we're done. */
			return (0);
		nameidone(&nd);
		vp = nd.ni_vp;

		/*
		 * Do a full sanity check on the vnode. If it isn't the
		 * mount point, we're done.
		 */
		if ((vp->v_flag & VROOT) == 0 ||
		    (mp = vnode_mount(vp)) == NULL ||
		    (vfsp = vfs_statfs(mp)) == NULL ||
		    strcmp(vfsp->f_fstypename, "newnfs") ||
		    strcmp(vfsp->f_mntonname, path) ||
		    (nmp = VFSTONFS(mp)) == NULL ||
		    (nmp->nm_state & NFSSTA_MOUNTED) == 0) {
			vnode_put(vp);
			return (0);
		}
		clp = nmp->nm_clp;
		vnode_put(vp);
		lck_spin_lock(nfscl_renew_slock);
		if (clp != NULL) {
			/*
			 * Search the list of clients that need a renew
			 * thread for the right one.
			 */
			LIST_FOREACH(rtp, &nfsrenewthreadhead, next) {
				if (rtp->clp == clp)
					break;
			}
		} else {
			rtp = NULL;
		}
		if (rtp == NULL) {
			/* No clientid for this mount yet, so sleep on it. */
			lck_spin_unlock(nfscl_renew_slock);
			error = tsleep(&nfsrenewthreadhead, PZERO | PCATCH,
			    "nfsclrenew", 0);
			if (error)
				return (error);
		}
	} while (rtp == NULL);
	LIST_REMOVE(rtp, next);
	lck_spin_unlock(nfscl_renew_slock);
	clp->nfsc_renewthread = p;
	FREE(rtp, M_NFSRENEWTHREAD);
	nfscl_renewthread(clp, clp->nfsc_renewthread);
	return (0);
}

/*
 * Called to start renew thread.
 * Put the clp on the linked list and then wake up a thread.
 */
APPLESTATIC void
nfscl_start_renewthread(struct nfsclclient *clp)
{
	struct nfsrenewthread *rtp;

	MALLOC(rtp, struct nfsrenewthread *, sizeof (struct nfsrenewthread),
	    M_NFSRENEWTHREAD, M_WAITOK);
	rtp->clp = clp;
	lck_spin_lock(nfscl_renew_slock);
	LIST_INSERT_HEAD(&nfsrenewthreadhead, rtp, next);
	lck_spin_unlock(nfscl_renew_slock);
	if ((clp->nfsc_nmp->nm_flag & (NFSMNT_KERB | NFSMNT_AUTOM)) ==
	    NFSMNT_KERB)
		wakeup(&nfsrenewthreadhead);
	else
		kernel_thread(kernel_task, start_nfscl);
}

/*
 * Handle wcc_data.
 * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
 * as the first Op after PutFH.
 * (For NFSv4, the postop attributes are after the Op, so they can't be
 *  parsed here. A separate call to nfscl_postop_attr() is required.)
 */
APPLESTATIC int
nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
    struct nfsvattr *nap, int *flagp, int *wccflagp, void *stuff)
{
	register u_int32_t *tl;
	struct nfsnode *np = VTONFS(vp);
	struct nfsattrstuff *asp = (struct nfsattrstuff *)stuff;
	struct nfsvattr nfsva;
	int error = 0;

	if (wccflagp != NULL)
		*wccflagp = 0;
	if (nd->nd_flag & ND_NFSV3) {
		*flagp = 0;
		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
		if (*tl == newnfs_true) {
			NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
			if (asp != NULL) {
				asp->premtime.tv_sec =
				    fxdr_unsigned(time_t, *(tl + 2));
				asp->premtime.tv_nsec =
				    fxdr_unsigned(time_t, *(tl + 3));
			}
		} else if (asp != NULL) {
			asp->premtime.tv_sec = 0;
			asp->premtime.tv_nsec = 0;
		}
		error = nfscl_postop_attr(nd, nap, flagp, stuff);
	} else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
	    == (ND_NFSV4 | ND_V4WCCATTR)) {
		error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
		    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
		    NULL, NULL, NULL, NULL, NULL);
		if (error)
			return (error);
		/*
		 * Get rid of Op# and status for next op.
		 */
		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
		if (*(tl + 1))
			nd->nd_flag |= ND_NOMOREDATA;
		if (asp != NULL)
			asp->premtime = nfsva.na_vattr.nva_mtime;
	}
	if (wccflagp != NULL && asp != NULL &&
	    nfstimespeccmp(&np->n_mtime, &asp->premtime, ==))
		*wccflagp = 1;
nfsmout:
	return (error);
}

/*
 * Get postop attributes.
 */
APPLESTATIC int
nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp,
    __unused void *stuff)
{
	register u_int32_t *tl;
	int error = 0;

	*retp = 0;
	if (nd->nd_flag & ND_NOMOREDATA)
		return (error);
	if (nd->nd_flag & ND_NFSV3) {
		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
		*retp = fxdr_unsigned(int, *tl);
	} else if (nd->nd_flag & ND_NFSV4) {
		/*
		 * For NFSv4, the postop attr are at the end, so no point
		 * in looking if nd_repstat != 0.
		 */
		if (!nd->nd_repstat) {
			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
			if (*(tl + 1))
				/* should never happen since nd_repstat != 0 */
				nd->nd_flag |= ND_NOMOREDATA;
			else
				*retp = 1;
		}
	} else if (!nd->nd_repstat) {
		/* For NFSv2, the attributes are here iff nd_repstat == 0 */
		*retp = 1;
	}
	if (*retp) {
		error = nfsm_loadattr(nd, nap, NULL);
		if (error)
			*retp = 0;
	}
nfsmout:
	return (error);
}

/*
 * Fill in the setable attributes. The full argument indicates whether
 * to fill in them all or just mode and time.
 */
APPLESTATIC void
nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap,
    struct vnode *vp, int flags, u_int32_t rdev, struct ucred *cred, proc_t p)
{
	register u_int32_t *tl;
	register struct nfsv2_sattr *sp;
	nfsattrbit_t attrbits;
	struct timeval now;
	struct timespec neg1time = { -1, -1 };
	kauth_acl_t aclp = NULL;
	struct nfs_vattr nva;
	int error, alloced = 0;

	switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
	case ND_NFSV2:
		NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
		if (VATTR_IS_ACTIVE(vap, va_mode))
			sp->sa_mode = vtonfsv2_mode(vnode_vtype(vp),
			    vap->va_mode);
		else
			sp->sa_mode = vtonfsv2_mode(vnode_vtype(vp), 0600);
		if (VATTR_IS_ACTIVE(vap, va_uid))
			sp->sa_uid = txdr_unsigned(vap->va_uid);
		else
			sp->sa_uid = newnfs_xdrneg1;
		if (VATTR_IS_ACTIVE(vap, va_gid))
			sp->sa_gid = txdr_unsigned(vap->va_gid);
		else
			sp->sa_gid = newnfs_xdrneg1;
		if (flags & NFSSATTR_SIZE0)
			sp->sa_size = 0;
		else if (flags & NFSSATTR_SIZENEG1)
			sp->sa_size = newnfs_xdrneg1;
		else if (flags & NFSSATTR_SIZERDEV)
			sp->sa_size = txdr_unsigned(rdev);
		else if (VATTR_IS_ACTIVE(vap, va_data_size))
			sp->sa_size = txdr_unsigned(vap->va_data_size);
		else
			sp->sa_size = newnfs_xdrneg1;
		if (VATTR_IS_ACTIVE(vap, va_access_time))
			txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
		else
			txdr_nfsv2time(&neg1time, &sp->sa_atime);
		if (VATTR_IS_ACTIVE(vap, va_modify_time))
			txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
		else
			txdr_nfsv2time(&neg1time, &sp->sa_mtime);
		break;
	case ND_NFSV3:
		if (VATTR_IS_ACTIVE(vap, va_mode)) {
			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
			*tl++ = newnfs_true;
			*tl = txdr_unsigned(vap->va_mode);
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = newnfs_false;
		}
		if (VATTR_IS_ACTIVE(vap, va_uid)) {
			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
			*tl++ = newnfs_true;
			*tl = txdr_unsigned(vap->va_uid);
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = newnfs_false;
		}
		if (VATTR_IS_ACTIVE(vap, va_gid)) {
			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
			*tl++ = newnfs_true;
			*tl = txdr_unsigned(vap->va_gid);
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = newnfs_false;
		}
		if (VATTR_IS_ACTIVE(vap, va_data_size)) {
			NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
			*tl++ = newnfs_true;
			txdr_hyper(vap->va_data_size, tl);
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = newnfs_false;
		}
		microtime(&now);
		if (VATTR_IS_ACTIVE(vap, va_access_time)) {
			if (vap->va_access_time.tv_sec != now.tv_sec) {
				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
				txdr_nfsv3time(&vap->va_access_time, tl);
			} else {
				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
			}
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
		}
		if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
			if (vap->va_modify_time.tv_sec != now.tv_sec) {
				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
				txdr_nfsv3time(&vap->va_modify_time, tl);
			} else {
				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
			}
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
		}
		break;
	case ND_NFSV4:
		NFSZERO_ATTRBIT(&attrbits);
		if (VATTR_IS_ACTIVE(vap, va_mode))
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE);
		if (VATTR_IS_ACTIVE(vap, va_uid))
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
		if (VATTR_IS_ACTIVE(vap, va_gid))
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP);
		if (VATTR_IS_ACTIVE(vap, va_data_size))
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
		if (VATTR_IS_ACTIVE(vap, va_access_time))
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET);
		if (VATTR_IS_ACTIVE(vap, va_modify_time))
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET);
		if (VATTR_IS_ACTIVE(vap, va_acl)) {
			/*
			 * For NFSv4, ACLs can't be deleted, but I believe that
			 * setting the ACL to the canonical 6 ACEs based on the
			 * file's mode bits will achieve the equivalent.
			 */
			if (vap->va_acl == NULL) {
				error = ncl_getattr(vp, &nva, cred, p, NULL);
				if (error == 0)
					aclp = nfsacl_from_mode(nva.nva_mode,
					    NULL);
				if (aclp != NULL) {
					NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
					alloced = 1;
				}
			} else {
				aclp = vap->va_acl;
				NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
			}
		}
		(void) nfsv4_fillattr(nd, vp, vap, NULL, 0, &attrbits,
		    NULL, NULL, NULL, aclp);
		if (alloced)
			kauth_acl_free(aclp);
		break;
	};
}

/*
 * nfscl_request() - mostly a wrapper for newnfs_request().
 */
APPLESTATIC int
nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
    struct ucred *cred, void *stuff)
{
	register int ret, vers;
	struct nfsmount *nmp;
	struct nfsattrstuff *asp = (struct nfsattrstuff *)stuff;

	nmp = VFSTONFS(vp->v_mount);
	if (nd->nd_flag & ND_NFSV4)
		vers = NFS_VER4;
	else if (nd->nd_flag & ND_NFSV3)
		vers = NFS_VER3;
	else
		vers = NFS_VER2;
	if (asp != NULL)
		ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p,
		    cred, NFS_PROG, vers, NULL, 1, &asp->xid);
	else
		ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p,
		    cred, NFS_PROG, vers, NULL, 1, NULL);
	return (ret);
}

/*
 * Load the nfs_statfs stuff into the generic structures.
 */
APPLESTATIC void
nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *mystatfs)
{
	struct vfsstatfs *sbp = (struct vfsstatfs *)mystatfs;

	sbp->f_flags = nmp->nm_flag;
	sbp->f_iosize = NFS_IOSIZE;
	if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
		/*
		 * Adjust block size to get total block count to fit in a long.
		 * If we can't increase block size enough, clamp to max long.
		 */
		u_quad_t tquad, tquad2, bsize;
		bsize = NFS_FABLKSIZE;

		tquad = sfp->sf_tbytes;
		tquad /= bsize;
		while ((tquad & ~0x7fffffff) && (bsize < 0x40000000)) {
			bsize <<= 1;
			tquad >>= 1;
		}
		sbp->f_blocks = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;

		tquad = sfp->sf_fbytes;
		tquad /= bsize;
		sbp->f_bfree = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;

		tquad = sfp->sf_abytes;
		tquad /= bsize;
		sbp->f_bavail = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;

		sbp->f_bsize = (long)bsize;

		/* adjust file slots too... */
		tquad = sfp->sf_tfiles;
		tquad2 = sfp->sf_ffiles;
		while (tquad & ~0x7fffffff) {
			tquad >>= 1;
			tquad2 >>= 1;
		}
		sbp->f_files = tquad;
		sbp->f_ffree = tquad2;
	} else {
		sbp->f_bsize = (long)sfp->sf_bsize;
		sbp->f_blocks = (long)sfp->sf_blocks;
		sbp->f_bfree = (long)sfp->sf_bfree;
		sbp->f_bavail = (long)sfp->sf_bavail;
		sbp->f_files = 0;
		sbp->f_ffree = 0;
	}
}

/*
 * Set up local stuff, based on fsinfo.
 */
APPLESTATIC void
nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp)
{
	u_int32_t maxsize, prefsize;

	prefsize = fsp->fs_wtpref;
	if (nmp->nm_wsize == 0)
		nmp->nm_wsize = (prefsize + NFS_FABLKSIZE - 1) &
		    ~(NFS_FABLKSIZE - 1);
	maxsize = fsp->fs_wtmax;
	if ((int)maxsize < nmp->nm_wsize) {
		nmp->nm_wsize = maxsize & ~(NFS_FABLKSIZE - 1);
		if (nmp->nm_wsize == 0)
			nmp->nm_wsize = maxsize;
	}
	if (nmp->nm_wsize > NFS_MAXBSIZE)
		nmp->nm_wsize = NFS_MAXBSIZE;
	prefsize = fsp->fs_rtpref;
	if (nmp->nm_rsize == 0)
		nmp->nm_rsize = (prefsize + NFS_FABLKSIZE - 1) &
		    ~(NFS_FABLKSIZE - 1);
	maxsize = fsp->fs_rtmax;
	if ((int)maxsize < nmp->nm_rsize) {
		nmp->nm_rsize = maxsize & ~(NFS_FABLKSIZE - 1);
		if (nmp->nm_rsize == 0)
			nmp->nm_rsize = maxsize;
	}
	if (nmp->nm_rsize > NFS_MAXBSIZE)
		nmp->nm_rsize = NFS_MAXBSIZE;
	prefsize = fsp->fs_dtpref;
	if (prefsize == 0)
		prefsize = NFS_READDIRSIZE;
	if (prefsize > NFS_MAXBSIZE)
		prefsize = NFS_MAXBSIZE;
	if (nmp->nm_readdirsize == 0)
		nmp->nm_readdirsize = (prefsize + NFS_FABLKSIZE - 1) &
		    ~(NFS_FABLKSIZE - 1);
	if ((int)prefsize < nmp->nm_readdirsize)
		nmp->nm_readdirsize = prefsize;
	if ((int)maxsize < nmp->nm_readdirsize) {
		nmp->nm_readdirsize = maxsize;
	}
	nmp->nm_fsinfo.maxfilesize = fsp->fs_maxfilesize;
	nmp->nm_fsinfo.fsproperties = fsp->fs_properties;
	nmp->nm_state |= NFSSTA_GOTFSINFO;
}

/*
 * Compute an entry in the NFS hash table structure
 */
APPLESTATIC u_long
ncl_hash(u_char *fhp, int fhsize)
{
	u_long fhsum;
	int i;

	fhsum = 0;
	for (i = 0; i < fhsize; i++)
		fhsum += fhp[i];
	return (fhsum);
}

/*
 * Initialize client data structures.
 */
APPLESTATIC void
nfscl_init(void)
{

	if (nfscl_inited)
		return;
	newnfs_portinit();
	nfs_clstate_lck_grp_attr = lck_grp_attr_alloc_init();
	nfs_clstate_lck_grp = lck_grp_alloc_init("nfs_clstate", nfs_clstate_lck_grp_attr);
	nfs_clstate_lck_attr = lck_attr_alloc_init();
	nfs_clstate_mutex = lck_mtx_alloc_init(nfs_clstate_lck_grp, nfs_clstate_lck_attr);
	nfs_gsscl_lck_grp_attr = lck_grp_attr_alloc_init();
	nfs_gsscl_lck_grp = lck_grp_alloc_init("nfs_gsscl", nfs_gsscl_lck_grp_attr);
	nfs_gsscl_lck_attr = lck_attr_alloc_init();
	nfs_gsscl_mutex = lck_mtx_alloc_init(nfs_gsscl_lck_grp, nfs_gsscl_lck_attr);
	nfscl_renew_lck_grp_attr = lck_grp_attr_alloc_init();
	nfscl_renew_lck_grp = lck_grp_alloc_init("nfscl_renew", nfscl_renew_lck_grp_attr);
	nfscl_renew_lck_attr = lck_attr_alloc_init();
	nfscl_renew_slock = lck_spin_alloc_init(nfscl_renew_lck_grp, nfscl_renew_lck_attr);
	ncl_nfsnode_lck_grp_attr = lck_grp_attr_alloc_init();
	ncl_nfsnode_lck_grp = lck_grp_alloc_init("ncl_nfsnode", ncl_nfsnode_lck_grp_attr);
	ncl_nfsnode_lck_attr = lck_attr_alloc_init();
	ncl_nfsnode_slock = lck_spin_alloc_init(ncl_nfsnode_lck_grp, ncl_nfsnode_lck_attr);
	nfscl_inited = 1;
}

/*
 * Glue routine to do a getattr.
 * (For now, just get the fileid.)
 */
APPLESTATIC int
nfscl_getattr(struct vnode *vp, struct nfs_vattr *nap, struct ucred *cred, NFSPROC_T *p)
{
	struct vfs_context context;
	struct vnode_attr at;
	int error;

	context.vc_ucred = cred;
	context.vc_thread = current_thread();
	VATTR_INIT(&at);
	VATTR_WANTED(&at, va_fileid);

	error = vnode_getattr(vp, &at, &context);
	if (!error) {
		nap->nva_fileid = at.va_fileid;
	}
	return (error);
}

/*
 * Get a pointer to my IP addrress and return it.
 * Return NULL if you can't find one.
 */
u_int8_t *
nfscl_getmyip(__unused struct nfsmount *nmp, int *isinet6)
{
	struct in_ifaddr *ia;

	*isinet6 = 0;
	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
		if (IA_SIN(ia)->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
			return ((u_int8_t *)&(IA_SIN(ia)->sin_addr.s_addr));
	}
	return (NULL);
}

/*
 * copies a uio scatter/gather list to an mbuf chain.
 * NOTE: can ony handle iovcnt == 1
 */
APPLESTATIC void
nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz)
{
	char *uiocp;
	mbuf_t mp, mp2;
	int xfer, left, mlen, mplen;
	int uiosiz, clflg, rem, error;
	char *cp;

	if (uiop->uio_iovcnt != 1)
		panic("nfsm_uiotombuf: iovcnt != 1");

DEBUG1PRINTF("ncl_mbuflen=%d siz=%d\n",ncl_mbuf_mlen,siz);
	if (siz > ncl_mbuf_mlen)		/* or should it >= MCLBYTES ?? */
		clflg = 1;
	else
		clflg = 0;
	rem = NFSM_RNDUP(siz) - siz;
	mp = mp2 = nd->nd_mb;
	mplen = mbuf_len(mp);
DEBUG1PRINTF("mplen=%d\n",mplen);
	while (siz > 0) {
		// LP64todo - fix this!
		left = uio_iov_len(uiop);
		uiocp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
		if (left > siz)
			left = siz;
		if (left == 0)
			panic("nfsuiomb");
		uiosiz = left;
		while (left > 0) {
DEBUG1PRINTF("lpleft=%d\n",left);
			mlen = mbuf_trailingspace(mp);
DEBUG1PRINTF("aft mbuftr=%d\n",mlen);
			if (mlen == 0) {
				/*
				 * XXX Since callers can't deal with a failure,
				 * I'll just loop around, sleeping on lbolt.
				 */
				do {
				    mp = NULL;
DEBUG1PRINTF("atmgetcl=%d\n",clflg);
				    if (clflg)
					error = mbuf_mclget(MBUF_WAITOK,
					    MBUF_TYPE_DATA, &mp);
				    else
					error = mbuf_get(MBUF_WAITOK,
					    MBUF_TYPE_DATA, &mp);
DEBUG1PRINTF("aftmget=%d\n",error);
				    if (!error)
					error = mbuf_setnext(mp2, mp);
				    if (error) {
					printf("mberr=%d\n",error);
					(void) tsleep(&lbolt, PZERO,
					    "nfsmuiomb", 0);
				    }
				} while (error);
				mplen = 0;
				mp2 = mp;
DEBUG1PRINTF("at mbuftr\n");
				mlen = mbuf_trailingspace(mp);
DEBUG1PRINTF("aft mbuftr=%d\n",mlen);
			}
			xfer = (left > mlen) ? mlen : left;
			if (UIO_SEG_IS_USER_SPACE(uiop->uio_segflg))
				copyin(CAST_USER_ADDR_T(uiocp),
				    (caddr_t)mbuf_data(mp) + mplen, xfer);
			else
				bcopy(uiocp, (caddr_t)mbuf_data(mp) + mplen,
				    xfer);
DEBUG1PRINTF("xfer=%d\n",xfer);
			mplen += xfer;
			mbuf_setlen(mp, mplen);
			left -= xfer;
			uiocp += xfer;
			uiop->uio_offset += xfer;
			uio_uio_resid_add(uiop, -xfer);
		}
		uio_iov_base_add(uiop, uiosiz);
		uio_iov_len_add(uiop, -uiosiz);
		siz -= uiosiz;
	}
DEBUG1PRINTF("aft lp\n");
	if (rem > 0) {
		if (rem > (int)mbuf_trailingspace(mp)) {
			/* XXX again, just loop on lbolt */
			do {
DEBUG1PRINTF("atmget2\n");
				error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA,
				    &mp);
DEBUG1PRINTF("aftmget2=%d\n",error);
				if (!error)
					error = mbuf_setnext(mp2, mp);
				if (error) {
					printf("mberr2=%d\n",error);
					(void) tsleep(&lbolt, PZERO,
					    "nfsmuiomb", 0);
				}
			} while (error);
			mplen = 0;
		}
		cp = (caddr_t)mbuf_data(mp) + mplen;
		for (left = 0; left < rem; left++)
			*cp++ = '\0';
		mplen += rem;
		mbuf_setlen(mp, mplen);
		nd->nd_bpos = cp;
	} else {
		nd->nd_bpos = (caddr_t)mbuf_data(mp) + mplen;
	}
	nd->nd_mb = mp;
}

/*
 * Add a socket to the gsscl list.
 */
APPLESTATIC int
nfsgsscl_addport(u_short port, uid_t uid, NFSPROC_T *p)
{
	register struct nfssockreq *rp;
	register struct sockaddr_in *ad;
	struct nfssockreq sq;
	struct nfsgssclsock *ngcp, *gcp;
	int error;

	/*
	 * For now, only allow one gsscl to be running.
	 */
	NFSLOCKGSSCL;
	if (!LIST_EMPTY(&nfsgssclsockhead)) {
		gcp = LIST_FIRST(&nfsgssclsockhead);
		if (gcp->nfsgsscl_uid != uid || LIST_NEXT(gcp, nfsgsscl_list) !=
		    LIST_END(&nfsgssclsockhead)) {
			NFSUNLOCKGSSCL;
			return (ENXIO);
		}
	}
	NFSUNLOCKGSSCL;
	gcp = NULL;

	/*
	 * Set up the socket record and connect.
	 */
	rp = &sq;
	rp->nr_so = NULL;
	rp->nr_sotype = SOCK_DGRAM;
	rp->nr_soproto = IPPROTO_UDP;
	rp->nr_lock = (NFSR_RESERVEDPORT | NFSR_LOCALHOST);
	NFSSOCKADDRALLOC(rp->nr_nam);
	NFSSOCKADDRSIZE(rp->nr_nam, sizeof (struct sockaddr_in));
	ad = NFSSOCKADDR(rp->nr_nam, struct sockaddr_in *);
	ad->sin_family = AF_INET;
	ad->sin_addr.s_addr = htonl((u_int32_t)0x7f000001);	/* 127.0.0.1 */
	ad->sin_port = port;
	error = newnfs_connect(NULL, rp, NULL, NFSPROCCRED(p), p);
	if (error) {
		NFSSOCKADDRFREE(rp->nr_nam);
		return (error);
	}

	/*
	 * Connected, so now add to the list.
	 */
	MALLOC(ngcp, struct nfsgssclsock *, sizeof (struct nfsgssclsock),
	    M_NFSGSSCL, M_WAITOK);
	NFSBCOPY((caddr_t)rp, (caddr_t)&ngcp->nfsgsscl_sock,
	    sizeof (struct nfssockreq));
	ngcp->nfsgsscl_uid = uid;
	ngcp->nfsgsscl_ref = 0;
	NFSLOCKGSSCL;
	LIST_FOREACH(gcp, &nfsgssclsockhead, nfsgsscl_list) {
		if (gcp->nfsgsscl_uid == uid) {
			LIST_REMOVE(gcp, nfsgsscl_list);
			break;
		}
	}
	LIST_INSERT_HEAD(&nfsgssclsockhead, ngcp, nfsgsscl_list);
	NFSUNLOCKGSSCL;
	if (gcp != LIST_END(&nfsgssclsockhead)) {
		newnfs_disconnect(&gcp->nfsgsscl_sock);
		NFSSOCKADDRFREE(gcp->nfsgsscl_sock.nr_nam);
		FREE((caddr_t)gcp, M_NFSGSSCL);
	}
	return (0);
}

/*
 * Delete a port for a gsscl.
 */
APPLESTATIC void
nfsgsscl_deleteport(uid_t uid)
{
	struct nfsgssclsock *gcp;

tryagain:
	NFSLOCKGSSCL;
	LIST_FOREACH(gcp, &nfsgssclsockhead, nfsgsscl_list) {
		if (gcp->nfsgsscl_uid == uid)
			break;
	}
	if (gcp == LIST_END(&nfsgssclsockhead)) {
		NFSUNLOCKGSSCL;
		return;
	}

	/*
	 * Now, wait for it to be unused.
	 */
	if (gcp->nfsgsscl_ref > 0) {
		msleep(gcp, nfs_gsscl_mutex, PDROP | PZERO, "nfsgsscl", NULL);
		goto tryagain;
	}
	LIST_REMOVE(gcp, nfsgsscl_list);
	NFSUNLOCKGSSCL;
	newnfs_disconnect(&gcp->nfsgsscl_sock);
	NFSSOCKADDRFREE(gcp->nfsgsscl_sock.nr_nam);
	FREE((caddr_t)gcp, M_NFSGSSCL);
}

/*
 * Initialize the data structures for the cb server.
 * Handshake with any new nfscbds starting up to avoid any chance of
 * corruption.
 */
APPLESTATIC void
nfsrvd_cbinit(int terminating, __unused NFSPROC_T *p)
{
	struct nfssvc_sock *slp, *nslp;

	NFSD_LOCK();
	if (newnfscb_sockhead_flag & SLP_INIT)
		panic("nfsd init");
	newnfscb_sockhead_flag |= SLP_INIT;
	if (terminating) {
		slp = TAILQ_FIRST(&newnfscb_sockhead);
		while (slp != TAILQ_END(&newnfscb_sockhead)) {
			nslp = TAILQ_NEXT(slp, ns_chain);
			if (slp->ns_flag & SLP_VALID)
				nfsrvd_zapsock(slp);
			TAILQ_REMOVE(&newnfscb_sockhead, slp, ns_chain);
			FREE((caddr_t)slp, M_NFSSVC);
			slp = nslp;
		}
	}
	NFSD_UNLOCK();

	TAILQ_INIT(&newnfscb_sockhead);
	TAILQ_INIT(&newnfscbd_head);
	newnfscbd_head_flag &= ~NFSD_CHECKSLP;

	MALLOC(newnfs_cbudpsock, struct nfssvc_sock *, sizeof (struct nfssvc_sock),
	    M_NFSSVC, M_WAITOK);
	NFSBZERO((caddr_t)newnfs_cbudpsock, sizeof (struct nfssvc_sock));
	NFSINITSLP(newnfs_cbudpsock);
	TAILQ_INSERT_HEAD(&newnfscb_sockhead, newnfs_cbudpsock, ns_chain);

	NFSD_LOCK();
	newnfscb_sockhead_flag &= ~SLP_INIT;
	if (newnfscb_sockhead_flag & SLP_WANTINIT) {
		newnfscb_sockhead_flag &= ~SLP_WANTINIT;
		wakeup((caddr_t)&newnfscb_sockhead);
	}
	NFSD_UNLOCK();
}

/*
 * Check each of the attributes to be set, to ensure they aren't already
 * the correct value. Disable setting ones already correct.
 * Return whether or not any attribute needs to be set.
 */
APPLESTATIC int
nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
{
	int ret = 0;

	if (VATTR_IS_ACTIVE(vap, va_mode)) {
		if (vap->va_mode == nvap->na_mode)
			VATTR_CLEAR_ACTIVE(vap, va_mode);
		else
			ret = 1;
	}
	if (VATTR_IS_ACTIVE(vap, va_uid)) {
		if (vap->va_uid == nvap->na_uid)
			VATTR_CLEAR_ACTIVE(vap, va_uid);
		else
			ret = 1;
	}
	if (VATTR_IS_ACTIVE(vap, va_gid)) {
		if (vap->va_gid == nvap->na_gid)
			VATTR_CLEAR_ACTIVE(vap, va_gid);
		else
			ret = 1;
	}
	if (VATTR_IS_ACTIVE(vap, va_data_size)) {
		if (vap->va_data_size == nvap->na_size)
			VATTR_CLEAR_ACTIVE(vap, va_data_size);
		else
			ret = 1;
	}
	if (VATTR_IS_ACTIVE(vap, va_access_time)) {
		if (vap->va_access_time.tv_sec == nvap->na_atime.tv_sec &&
		    vap->va_access_time.tv_nsec == nvap->na_atime.tv_nsec)
			VATTR_CLEAR_ACTIVE(vap, va_access_time);
		else
			ret = 1;
	}
	if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
		if (vap->va_modify_time.tv_sec == nvap->na_mtime.tv_sec &&
		    vap->va_modify_time.tv_nsec == nvap->na_mtime.tv_nsec)
			VATTR_CLEAR_ACTIVE(vap, va_modify_time);
		else
			ret = 1;
	}
	return (ret);
}

#ifdef APPLEKEXT
/*
 * Check to see if the process for this owner exists. Return 1 if it doesn't
 * and 0 otherwise.
 */
static int
nfscl_procdoesntexist(u_int8_t *own)
{
	union {
		u_int32_t	lval;
		u_int8_t	cval[4];
	} tl;
	proc_t p;
	pid_t pid;
	int ret = 0;

	tl.cval[0] = *own++;
	tl.cval[1] = *own++;
	tl.cval[2] = *own++;
	tl.cval[3] = *own++;
	pid = tl.lval;
	p = pfind_locked(pid);
	if (p == PROC_NULL)
		return (1);
	if (p->p_stats == NULL)
		return (0);
	tl.cval[0] = *own++;
	tl.cval[1] = *own++;
	tl.cval[2] = *own++;
	tl.cval[3] = *own++;
	if (tl.lval != p->p_stats->p_start.tv_sec) {
		ret = 1;
	} else {
		tl.cval[0] = *own++;
		tl.cval[1] = *own++;
		tl.cval[2] = *own++;
		tl.cval[3] = *own;
		if (tl.lval != p->p_stats->p_start.tv_usec)
			ret = 1;
	}
	return (ret);
}
#endif	/* APPLEKEXT */

/*
 * Called by nfssvc() to provide a bsd process/thread that can be used
 * as an async I/O thread. For gssd upcalls to work,
 * a kernel thread isn't sufficient.
 */
static int
nfscl_asyncioproc(proc_t p, struct nfssvc_args *uap)
{
	struct nfsasynciothread *atp;
	struct vfsstatfs *vfsp;
	mount_t mp;
	struct nfsmount *nmp;
	vnode_t vp;
	int error;
	struct nameidata nd;
	char path[MAXPATHLEN];
	u_int pathlen;

	error = copyinstr(uap->argp, path, MAXPATHLEN, (size_t *)&pathlen);
	if (error)
		return (error);

	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
	    CAST_USER_ADDR_T(path), vfs_context_current());
	error = namei(&nd);
	if (error)
		/* If the mount point doesn't exist, we're done. */
		return (0);
	nameidone(&nd);
	vp = nd.ni_vp;

	/*
	 * Do a full sanity check on the vnode. If it isn't the
	 * mount point, we're done.
	 */
	if ((vp->v_flag & VROOT) == 0 ||
	    (mp = vnode_mount(vp)) == NULL ||
	    (vfsp = vfs_statfs(mp)) == NULL ||
	    strcmp(vfsp->f_fstypename, "newnfs") ||
	    strcmp(vfsp->f_mntonname, path) ||
	    (nmp = VFSTONFS(mp)) == NULL ||
	    (nmp->nm_state & NFSSTA_MOUNTED) == 0) {
		vnode_put(vp);
		return (0);
	}
	MALLOC(atp, struct nfsasynciothread *, sizeof (struct nfsasynciothread),
	    M_TEMP, M_WAITOK);
	atp->nfsio_ready = 0;
	lck_mtx_lock(ncl_iod_mutex);
	TAILQ_INSERT_HEAD(&nmp->nm_asyncio, atp, nfsio_list);
	nmp->nm_numasync++;
	lck_mtx_unlock(ncl_iod_mutex);
	vnode_put(vp);
	newnfs_iod_continue(nmp, atp, p);
	lck_mtx_lock(ncl_iod_mutex);
	TAILQ_REMOVE(&nmp->nm_asyncio, atp, nfsio_list);
	nmp->nm_numasync--;
	if (TAILQ_EMPTY(&nmp->nm_asyncio)) {
		lck_mtx_unlock(ncl_iod_mutex);
		wakeup((caddr_t)&nmp->nm_asyncio);
	} else {
		lck_mtx_unlock(ncl_iod_mutex);
	}
	FREE(atp, M_NFSRENEWTHREAD);
	return (0);
}

/*
 * Called to start up a async I/O thread.
 * Used when iothreads > 0 for a mount, unless it is a non-automounted
 * Kerberized mount. (That case is handled via nfscl_asyncioproc().)
 */
static void
start_nfsio(void)
{
	struct nfsasynciothread *atp;
	struct nfsmount *nmp;

	lck_mtx_lock(ncl_iod_mutex);
	atp = TAILQ_FIRST(&nfsasynciothreadhead);
	if (atp != NULL) {
		TAILQ_REMOVE(&nfsasynciothreadhead, atp, nfsio_list);
		atp->nfsio_ready = 0;
		nmp = atp->nfsio_nmp;
		TAILQ_INSERT_HEAD(&nmp->nm_asyncio, atp, nfsio_list);
		nmp->nm_numasync++;
		lck_mtx_unlock(ncl_iod_mutex);
		newnfs_iod_continue(nmp, atp, NULL);
		lck_mtx_lock(ncl_iod_mutex);
		TAILQ_REMOVE(&nmp->nm_asyncio, atp, nfsio_list);
		nmp->nm_numasync--;
		if (TAILQ_EMPTY(&nmp->nm_asyncio)) {
			lck_mtx_unlock(ncl_iod_mutex);
			wakeup((caddr_t)&nmp->nm_asyncio);
		} else {
			lck_mtx_unlock(ncl_iod_mutex);
		}
		FREE(atp, M_NFSRENEWTHREAD);
	} else {
		lck_mtx_unlock(ncl_iod_mutex);
	}
	(void) thread_terminate(current_thread());
}

/*
 * Called to start async I/O thread.
 * Put the nmp on the linked list and then start up a thread.
 */
APPLESTATIC void
nfscl_start_iothread(struct nfsmount *nmp)
{
	struct nfsasynciothread *atp;

	MALLOC(atp, struct nfsasynciothread *, sizeof (struct nfsasynciothread),
	    M_TEMP, M_WAITOK);
	atp->nfsio_nmp = nmp;
	lck_mtx_lock(ncl_iod_mutex);
	TAILQ_INSERT_HEAD(&nfsasynciothreadhead, atp, nfsio_list);
	lck_mtx_unlock(ncl_iod_mutex);
	kernel_thread(kernel_task, start_nfsio);
}

APPLESTATIC int
nfs_catnap(int prio, const char *wmesg)
{
	int retv;

	retv = tsleep(&lbolt, prio, wmesg, 0);
	return (retv);
}

